<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>缓存一个新变量到存储中</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ref.apcu.html">« APCu 函数</a></li>
      <li style="float: right;"><a href="function.apcu-cache-info.html">apcu_cache_info »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.apcu.html">APCu 函数</a></li>
    <li>缓存一个新变量到存储中</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.apcu-add" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">apcu_add</h1>
  <p class="verinfo">(PECL apcu &gt;= 4.0.0)</p><p class="refpurpose"><span class="refname">apcu_add</span> &mdash; <span class="dc-title">
   缓存一个新变量到存储中
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.apcu-add-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>apcu_add</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$key</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$ttl</code><span class="initializer"> = 0</span></span>): <span class="type">bool</span></div>

  <div class="methodsynopsis dc-description"><span class="methodname"><strong>apcu_add</strong></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$values</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$unused</code><span class="initializer"> = NULL</span></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$ttl</code><span class="initializer"> = 0</span></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   将一个从未被缓存过的变量添加到到存储中。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    与 PHP 中常见的变量生命周期不同的是，通过 <span class="function"><strong>apcu_add()</strong></span> 存储的变量可以在多个 request 之间共享（直到该变量从 cache 中被删除）。
   </span>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.apcu-add-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">key</code></dt>

     <dd>

      <p class="para">
       使用此名称存储变量。<code class="parameter">key</code> 必须是唯一的，因此如果用 <span class="function"><strong>apcu_add()</strong></span> 存储变量时指定的 key 已经存在， 就会直接返回 <strong><code>false</code></strong>，不会覆写已有的数据。（这也是 <span class="function"><strong>apcu_add()</strong></span> 和 <span class="function"><a href="function.apcu-store.html" class="function">apcu_store()</a></span> 之间唯一的区别。）
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">var</code></dt>

     <dd>

      <p class="para">
       被存储的变量
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">ttl</code></dt>

     <dd>

      <p class="para">
       变量生存时间（Time To Live）；被存储的 <code class="parameter">var</code> 经过 <code class="parameter">ttl</code> 秒后，会从存储中被删除（下一次请求时）。如果没提供 <code class="parameter">ttl</code> （或 <code class="parameter">ttl</code> 为 <code class="literal">0</code> ），该变量会一直存在直到手动删除它，或者其他原因导致该变量从缓存中消失（清除，重启等等。）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">values</code></dt>

     <dd>

      <p class="para">
       数组索引作为 key，数组值作为被存储的 var。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.apcu-add-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   变量被成功添加时返回 TRUE，否则返回 FALSE。第二种语法返回包含添加失败的 key 的数组。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.apcu-add-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-499">
    <p><strong>示例 #1  <span class="function"><strong>apcu_add()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'BAR'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">apcu_add</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">apcu_fetch</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'NEVER&nbsp;GETS&nbsp;SET'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">apcu_add</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">apcu_fetch</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(3) &quot;BAR&quot;
string(3) &quot;BAR&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.apcu-add-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.apcu-store.html" class="function" rel="rdfs-seeAlso">apcu_store()</a> - 缓存一个变量到存储中</span></li>
    <li class="member"><span class="function"><a href="function.apcu-fetch.html" class="function" rel="rdfs-seeAlso">apcu_fetch()</a> - Fetch a stored variable from the cache</span></li>
    <li class="member"><span class="function"><a href="function.apcu-delete.html" class="function" rel="rdfs-seeAlso">apcu_delete()</a> - Removes a stored variable from the cache</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>